package com.ls.blog.controller;


import com.ls.blog.dto.Recodes;
import com.ls.blog.entity.Links;
import com.ls.blog.service.TLinksService;
import com.ls.blog.utils.R;
import com.ls.blog.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lishun
 * @since 2022-01-14
 */
@RestController
@RequestMapping("/links")
@Api(tags = "友链模块")
public class LinksController {
    @Autowired
    TLinksService tLinksService;


    /**
     * 新增友链
     * @param links
     * @return
     */
    @PostMapping("/saveLink")
    public Result saveLink(@RequestBody Links links){
        links.setCreateTime(LocalDateTime.now());
        Boolean result = tLinksService.save(links);
        return R.ok();

    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/getAllLink")
    public Result<List<Links>> getAllLink(){
        List<Links> linksList = tLinksService.list();
        return R.ok(linksList);
    }

    /**
     * 友链的分页查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getLinksByPage")
    @ApiOperation("友链的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "每页的数量"),
    })
    public Result<Recodes<Links>> getLinksByPage(Long current, Long size){
        return R.ok(tLinksService.pageLinks(current, size));
    }

    /**
     * 通过名称查找友链
     * @param name
     * @return
     */
    @GetMapping("/getLinksByName")
    public Result<List<Links>> getLinksByName(String name){
        return R.ok(tLinksService.getLinksByName(name));
    }

    /**
     * 更新友链
     * @param links
     * @return
     */
    @PutMapping("/updateLink")
    public Result updateLink(@RequestBody Links links){
        tLinksService.updateLink(links);
        return R.ok();
    }

    /**
     * 删除友链
     * @param id
     * @return
     */
    @DeleteMapping("deleteLinkById")
    public Result deleteLinkById(Long id){
        tLinksService.deleteLinkById(id);
        return R.ok();
    }

    /**
     * 根据id查询友链
     * @param id
     * @return
     */
    @GetMapping("/getLinksById")
    public Result<Links> getLinksById(Long id){
        return R.ok(tLinksService.getLinksById(id));
    }
}

